//
//  FlutterIosMapViewFactory.m
//  o_amap
//
//  Created by Q神 on 2021/4/9.
//

#import "FlutterIosMapViewFactory.h"
#import "FlutterIosMapView.h"

@implementation FlutterIosMapViewFactory {
    NSObject<FlutterPluginRegistrar> * _registrar;
    NSObject<FlutterPlugin> * _mapPlugin;
    GdMapFlutterStreamHandler * _handler;
}

- (instancetype)initWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar withMapPlugin:(NSObject<FlutterPlugin> * _Nonnull)mapPlugin streamHandler:(GdMapFlutterStreamHandler* _Nullable)handler {
    
    self = [super init];
    if (self) {
        _registrar = registrar;
        _mapPlugin = mapPlugin;
        _handler = handler;
    }
    return self;
}

#pragma mark - FlutterPlatformViewFactory

/// 创建一个“FlutterPlatformView”
/// 由iOS代码实现，该代码公开了一个用于嵌入Flutter应用程序的“UIView”。
/// 这个方法的实现应该创建一个新的“UIView”并返回它。
/// @param frame Flutter通过其布局widget来计算得来
/// @param viewId 视图的唯一标识符，创建一个 UIKitView 该值会+1
/// @param args 对应Flutter 端UIKitView的creationParams参数
- (nonnull NSObject<FlutterPlatformView> *)createWithFrame:(CGRect)frame viewIdentifier:(int64_t)viewId arguments:(id _Nullable)args {
    
    /// args 为flutter 传过来的参数
     FlutterIosMapView *mapView = [[FlutterIosMapView alloc] initWithWithFrame:frame viewIdentifier:viewId arguments:args binaryRegistrar:_registrar withMapPlugin:_mapPlugin streamHandler:_handler];
    
    return mapView;
}

/// 设置参数的编码方式
/// 关于编码格式一定要和flutter那边统一，并且和传递的参数类型匹配，
/// 如果flutter传递的是json，map，编码必须是JSONMessageCodec，
/// flutter传递的是string，编码必须是Stringcodec，否则会出错，加载不了view
- (NSObject<FlutterMessageCodec> *)createArgsCodec {
   return [FlutterStandardMessageCodec sharedInstance];
}

@end
